package cn.edu.sdju.fengguoyu.ahoedgemonitor;

import cn.edu.sdju.fengguoyu.ahoedgemonitor.App;
import cn.edu.sdju.fengguoyu.ahoedgemonitor.CanvasMap;
import cn.edu.sdju.fengguoyu.ahoedgemonitor.Sprite;
import javafx.geometry.Bounds;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ServerSprite  extends Sprite {
//    Image server_icon = new Image(App.class.getResource( "server_icon.png").openStream());
    private static Image icon = null;
    private static Image icon_timeout = null;
    private static Image icon_error = null;
    protected String label = "Hello world.";

    protected ArrayList<String> hitArray = new ArrayList<>();
    public void setLabel(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    private CanvasMap m;
    public ServerSprite() {
        if (icon == null) {
            try {
                icon = new Image(App.class.getResource( "server_icon.png").openStream());
                icon_timeout = new Image(App.class.getResource( "server_icon_timeout.png").openStream());
                icon_error = new Image(App.class.getResource( "server_icon_error.png").openStream());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public ServerSprite(double x, double y) {
        this();
        this.x = x;
        this.y = y;
    }
    public ServerSprite(double x, double y, double width, double height) {
        this(x, y);
        this.width = width;
        this.height = height;
    }

    @Override
    List<String> getHitList() {
        return hitArray;
    }

    @Override
    public void draw(GraphicsContext gc) {
        switch (status){
            case 1:
                gc.drawImage(icon_timeout,x, y, width, height);
                break;
            case 2:
                gc.drawImage(icon_error,x, y, width, height);
                break;
            default:
                gc.drawImage(icon,x, y, width, height);
        }
        gc.setFont(new Font("Arial", 14));
        gc.setFill(Color.BLUE);
        Text text = new Text(label);
        text.setFont(new Font("Arial", 14));

        Bounds bounds = text.getBoundsInLocal();
        double textWidth = bounds.getWidth();
        gc.fillText(label,x - (textWidth/2) + (width/2), y+height+8);
    }

    public ServerSprite(CanvasMap m) {
        this.m = m;
    }

    public void setM(CanvasMap m) {
        this.m = m;
    }
}
